#include "shapeItem.h"


shapeItem::shapeItem(QObject *parent) :
    QObject(parent), QGraphicsItem(){}

shapeItem::~shapeItem(){}

QRectF shapeItem::boundingRect() const
{
    if(this->typeItem == "r")
    {
        return QRectF (-a/2, -b/2, a, b);
    }
    if(this->typeItem == "c")
    {
        return QRectF (-a/2, -a/2, a, a);
    }
    if(this->typeItem == "t")
    {
       return QRectF (-a/3, -b/2, a, b);
    }

}

QPainterPath shapeItem::shape() const
{
    QPainterPath path;
    if(this->typeItem == "r")
    {
        path.addRect(QRectF (-a/2, -a/2, a, a));
    }
    if(this->typeItem == "c")
    {
        path.addEllipse(-a/2, -a/2, a, a);
    }
    if(this->typeItem == "t")
    {
        QPainterPath trianglePath;
        QPoint p0(a/3, -b/2), p1(a/3, b/2), p2(-a/3, b/2);
        trianglePath.moveTo(p0);
        trianglePath.lineTo(p1);
        trianglePath.lineTo(p2);
        trianglePath.lineTo(p0);
        path.addPath(trianglePath);

    }
    return path;
}


void shapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
    if(this->typeItem == "r")
    {
        painter->setPen(pen);
        painter->setBrush(Qt::red);
        painter->drawRect(-a/2,-b/2, a, b);

        angel = this->rotation();
    }

    if(this->typeItem == "c")
    {
        painter->setPen(pen);
        painter->setBrush(Qt::yellow);
        painter->drawEllipse(-a/2, -a/2, a, a);

        angel = this->rotation();

    }

    if(this->typeItem == "t")
    {
        QPainterPath path;
        QPoint p0(a/3, -b/2), p1(a/3, b/2), p2(-a/3, b/2);
        path.moveTo(p0);
        path.lineTo(p1);
        path.lineTo(p2);
        path.lineTo(p0);
        painter->setPen(pen);
        painter->setBrush(Qt::blue);
        painter->drawPath(path);
        angel = this->rotation();

    }
    this->update(boundingRect());
    this->setFlag(ItemIsSelectable, true);
    this->setFlag(ItemIsFocusable,true);
    this->setFlag(ItemIsMovable,true);
    Q_UNUSED(option);
    Q_UNUSED(widget);
}

void shapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    this->setPos(mapToScene(event->pos()));
    QPointF point = this->pos();
    this->coord_x = point.x();
    this->coord_y = point.y();
}

void shapeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        this->setCursor(QCursor(Qt::ClosedHandCursor));
    }
    else if(event->button() == Qt::RightButton)
    {
        if(isSelected())
        {
            pen.setColor(Qt::black);
            pen.setStyle(Qt::SolidLine);
            pen.setWidth(1);
            this->setSelected(false);
        }
        else
        {
            pen.setColor(Qt::green);
            pen.setWidth(3);
            pen.setStyle(Qt::DashDotLine);
            this->setFlag(ItemIsSelectable, true);
            this->setSelected(true);
        }
    }
}


void shapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    this->setCursor(QCursor(Qt::ArrowCursor));
    Q_UNUSED(event);
}


